package com.fita.admin.web.controller.authority;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.fita.admin.entity.security.Login;
import com.fita.admin.service.authority.PrincipalService;
import com.fita.admin.web.resolver.Secure;
import com.fita.entity.principal.Principal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * 个人中心
 *
 * @author Ark
 *
 */
@Controller
@RequestMapping("/profile")
@MenuSetting("menu-authority")
public class ProfileController {

    private static Logger logger = LoggerFactory.getLogger(ProfileController.class);

    @Autowired
    private PrincipalService principalService;
    
    /**
     * 个人中心
     *
     * @param login
     * @return
     */
    @RequestMapping(value = "" , method = RequestMethod.GET)
    @MenuSetting("submenu-profile-info")
    public ModelAndView profile(@Secure Login login) {
                      
        ModelAndView modelAndView = new ModelAndView("authority/profile/profile");
        
        Principal profile = principalService.load(Principal.class, login.getId());
        
        modelAndView.addObject("id", login.getId());
        
        modelAndView.addObject("profile", profile);
        
        return modelAndView;
        
    }
    
    /**
     * @param id
     * @return
     */
    @RequestMapping(value="/{id}/change-password", method = RequestMethod.GET)
    public ModelAndView changePassword(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("authority/profile/change-password");
        
        modelAndView.addObject("id", id);
        
        return modelAndView;
    }
    
    /**
     * @param id
     * @param password
     * @return
     */
    @RequestMapping(value = "/{id}/change-password", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onChangePassword(@PathVariable("id") Long id,
                                                     @RequestParam("password") String password) {
        
        HttpResult result = new HttpResult();
        
        try {
            
            Principal principal = principalService.load(Principal.class, id);
            principalService.changePassword(principal, password);
            
            result.success().message("修改密码成功！");
        } catch (BusinessException e) {
            result.fail("500").message(e.getMessage());
        } catch (Exception e) {
            
            result.fail("500").message("修改密码失败，请稍后再试！");
            
            logger.error("onChangePassword() error", e);
            
        }
            
        return result;
        
    }
}
